﻿using CSW.Framework.Common;
using CSW.Framework.Common.Binding.Interfaces;
using DevExpress.XtraEditors.DXErrorProvider;

namespace CSW.Framework.UI.DevEx.Binding
{
    /// <summary>
    /// Business Object Error Provider for DevEx Grid
    /// </summary>
    public class DXBusinessObjectErrorProvider : IDXDataErrorInfo
    {
        private readonly IBusinessObject m_BusinessObject;

        /// <summary>
        /// Initializes a new instance of the <see cref="DXBusinessObjectErrorProvider"/> class.
        /// </summary>
        /// <param name="businessObject">The business object.</param>
        public DXBusinessObjectErrorProvider(IBusinessObject businessObject)
        {
            Guard.ArgumentNotNull(businessObject, "businessObject");

            m_BusinessObject = businessObject;
        }

        /// <summary>
        /// Returns information on an error associated with a business object.
        /// </summary>
        /// <param name="info">An <see cref="T:DevExpress.XtraEditors.DXErrorProvider.ErrorInfo"/> object that contains information on an error.</param>
        public void GetError(ErrorInfo info)
        {
        }

        /// <summary>
        /// Returns information on an error associated with a specific business object's property.
        /// </summary>
        /// <param name="propertyName">A string that identifies the name of the property for which information on an error is to be returned.</param>
        /// <param name="info">An <see cref="T:DevExpress.XtraEditors.DXErrorProvider.ErrorInfo"/> object that contains information on an error.</param>
        public void GetPropertyError(string propertyName, ErrorInfo info)
        {
            string errorMessage;
            if (!m_BusinessObject.CheckRules(propertyName, out errorMessage))
            {
                info.ErrorType = ErrorType.Warning;
                info.ErrorText = errorMessage;
            }
            else
            {
                info.ErrorType = ErrorType.None;
            }
        }
    }
}
